package syntaxtree;

import semant.Env;
import symbol.ClassInfo;
import symbol.Symbol;

public abstract class Type extends Absyn
{
    public boolean isComparable(Type t)
    {
        return t.getClass() == this.getClass();
    }
    
	public Type(int l, int r)
	{
		super(l, r);
	}
    public static boolean isCompatible(ClassInfo dest,ClassInfo source)
    {
        ClassInfo tmp = source;
        while (tmp != null){
            if(tmp.name == dest.name)
                return true;
            tmp = tmp.base;
        }
        return false;
    }

    public static final boolean compatible(Env e, Type target, Type source)
    {
        if(target instanceof IntegerType)
            return source instanceof IntegerType;
        else if (target instanceof IntArrayType)
           return source instanceof IntArrayType;
        else if (target instanceof BooleanType)
           return source instanceof BooleanType;

        if(!(source instanceof IdentifierType))
            return false;

        Symbol nc1 = Symbol.symbol(((IdentifierType)target).name);
        Symbol nc2 = Symbol.symbol(((IdentifierType)source).name);

        ClassInfo c1 = e.classes.get(nc1);
        ClassInfo c2 = e.classes.get(nc2);

        return compatible(e,c1,c2);
    }

    public static final boolean equal(Type target, Type source)
    {
        if(target instanceof IntegerType)
            return source instanceof IntegerType;
        else if (target instanceof IntArrayType)
           return source instanceof IntArrayType;
        else if (target instanceof BooleanType)
           return source instanceof BooleanType;

        if(!(source instanceof IdentifierType))
            return false;

        Symbol nc1 = Symbol.symbol(((IdentifierType)target).name);
        Symbol nc2 = Symbol.symbol(((IdentifierType)source).name);

        return nc1.toString().equals(nc2.toString());
    }

    private static final boolean compatible(Env e, ClassInfo s, ClassInfo d)
    {
        if(s == null)
            return false;
        while(d!=null)
        {
            if(s.name == d.name)
                return true;
            else
                d = d.base;
        }
        return false;


        
    }

}






